/**
 * Created by hiibook on 2017/6/15.
 */
var xmlReader =require("xmlreader");

module.exports = function (req,res,next) {
    console.log('微信Post消息解析');
        var _da;
        req.on("data", function (data) {
            /*微信服务器传过来的是xml格式的，是buffer类型，因为js本身只有字符串数据类型，所以需要通过toString把xml转换为字符串*/
            _da = data.toString("utf-8");
        });
        req.on("end", function () {
            //解析xml数据结构
            var MessageObject={};
            // console.log("接收到的数据",_da);
            // 解析数据
            xmlReader.read(_da,function (err,response) {
                if(err){
                    console.log("消息解析失败");
                    res.end();
                }else {
                    // console.log("消息解析成功",response);
                    MessageObject.ToUserName = response.xml.ToUserName.text();
                    MessageObject.FromUserName =  response.xml.FromUserName.text();
                    MessageObject.CreateTime =  response.xml.CreateTime.text();
                    MessageObject.MsgType =  response.xml.MsgType.text();

                    switch (MessageObject.MsgType){
                        case "text":
                            // 文本消息
                            console.log("解析文本类型xml");
                            MessageObject.Content =  response.xml.Content.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();
                            break;
                        case "image":
                            // 图片消息
                            console.log("解析图片类型xml");
                            MessageObject.PicUrl =  response.xml.PicUrl.text();
                            MessageObject.MediaId =  response.xml.MediaId.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();

                            break;
                        case "voice":
                            // 语音消息
                            MessageObject.MediaId =  response.xml.MediaId.text();
                            MessageObject.Format =  response.xml.Format.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();


                            break;
                        case "video":
                            // 视频消息
                            MessageObject.MediaId =  response.xml.MediaId.text();
                            MessageObject.ThumbMediaId =  response.xml.ThumbMediaId.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();

                            break;
                        case "shortvideo":
                            // 小视频消息
                            MessageObject.MediaId =  response.xml.MediaId.text();
                            MessageObject.ThumbMediaId =  response.xml.ThumbMediaId.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();
                            break;
                        case "location":
                            // 地理位置消息
                            MessageObject.Location_X =  response.xml.Location_X.text();
                            MessageObject.Location_Y =  response.xml.Location_Y.text();
                            MessageObject.Scale =  response.xml.Scale.text();
                            MessageObject.Label =  response.xml.Label.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();

                            break;
                        case "link":
                            // 链接消息
                            MessageObject.Title =  response.xml.Title.text();
                            MessageObject.Description =  response.xml.Description.text();
                            MessageObject.Url =  response.xml.Url.text();
                            MessageObject.MsgId =  response.xml.MsgId.text();

                        case "event":
                            // 事件消息
                            MessageObject.Event =  response.xml.Event.text();
                            switch (MessageObject.Event) {
                                case "subscribe":
                                    // 订阅
                                    MessageObject.subscribe = true;
                                    break;
                                case "unsubscribe":
                                    // 取消
                                    MessageObject.subscribe = false;

                            }
                            break;
                        default:
                            console.log("发现一条未知类型信息");

                            break;
                    }

                    // 所有解析出来的数据  交给这里处理
                    req.wechatMsg = MessageObject ;
                    next();

                }
            })


        });
};